﻿using System;
using System.Collections.Generic;
using System.Linq;
using AgileDashboard.AppCode;

namespace AgileDashboard.controls
{
    public enum DateSort
    {
        None,
        Ascending,
        Descending
    }

    public partial class Impediments : System.Web.UI.UserControl
    {
        private const string ProjectName = "Impediments"; // name of version one project that contains impediments

        public string Status { get; set; }

        private int itemsPerRow = 1; // default

        public int ItemsPerRow
        {
            get
            {
                return this.itemsPerRow;
            }

            set
            {
                this.itemsPerRow = value;
            }
        }

        public int MaxItems
        {
            get
            {
                return this.maxItems;
            }

            set
            {
                this.maxItems = value;
            }
        }

        public bool ShowDescription
        {
            get
            {
                return this.showDescription;
            }

            set
            {
                this.showDescription = value;
            }
        }

        public bool EmbedLinks
        {
            get
            {
                return this.embedLinks;
            }

            set
            {
                this.embedLinks = value;
            }
        }

        public DateSort SortByCreateDate { get; set; }

        private int maxItems = 5; // default

        private bool showDescription = true; // default

        private bool embedLinks = true; // default

        public Impediments()
        {
            this.SortByCreateDate = DateSort.None;
        }

        public bool ShowImpedimentAge { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            var dao = new StoryDao(new RestDataConnection());

            IEnumerable<Story> impediments = this.Status != null ? dao.GetAllStoriesInProjectByStatus(ProjectName, this.Status) : dao.GetAllActiveStoriesInProject(ProjectName);

            if (impediments != null)
            {
                switch (this.SortByCreateDate)
                {
                    case DateSort.Ascending:
                        impediments = impediments.OrderBy(story => story.CreateDate);
                        break;
                    case DateSort.Descending:
                        impediments = impediments.OrderByDescending(story => story.CreateDate);
                        break;
                }

                // if max items is 0, return all items
                if (this.maxItems != 0)
                {
                    impediments = impediments.Take(this.maxItems);
                }

                this.Repeater1.DataSource = impediments;

                this.Repeater1.DataBind();
            }
        }
    }
}